﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

namespace WowGuildSite.Armory
{
    public class ArmorySkillCollection : IEnumerable<ArmorySkill>
    {
        private ArmoryCharacter         character;
        private Hashtable               skills      = new Hashtable();

        public ArmorySkill              this[SkillKey key]
        {
            get
            {
                ParseSkills();
                return skills[key] as ArmorySkill;
            }
        }

        public bool                     HasSkill(SkillKey key)
        {
            return this[key] != null;
        }

        public bool                     CanUseSkill(SkillKey key)
        {
            ArmorySkill skill = this[key];
            if (skill != null)
            {
                return skill.Current > 0;
            }
            return false;
        }

        public IList<ArmorySkill>       GetByCategory(SkillCategory category)
        {
            List<ArmorySkill> result = new List<ArmorySkill>();

            foreach( ArmorySkill s in this )
            {
                if (s.Category == category)
                {
                    result.Add(s);
                }
            }
            return result;
        }

        public IEnumerator<ArmorySkill> GetEnumerator()
        {
            ParseSkills();

            foreach (ArmorySkill s in skills.Values )
            {
                yield return s;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        private void ParseSkills()
        {
            if (skills.Count > 0)
            {
                return;
            }

            IList<XmlElement> skillElements = character.SheetDocument.GetElements("/page/characterInfo/skillTab/skillCategory/skill");

            foreach (XmlElement skillElement in skillElements)
            {
                ArmorySkill skill = ArmorySkill.FromElement(skillElement);
                skills.Add(skill.Key,skill);
            }
        }

        internal ArmorySkillCollection( ArmoryCharacter character )
        {
            if (character == null)
            {
                throw new ArgumentNullException("character");
            }

            this.character = character;
        }
    }
}
